package com.mogujie.tt.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.mogujie.tt.R;
import com.mogujie.tt.ui.widget.QuanzQzBimp;

/**
 * Created by 50281 on 2015/9/21.
 */
public class QuanzQzPublishGridAdapter extends BaseAdapter {



    private LayoutInflater inflater; // 视图容器
    private int selectedPosition = -1;// 选中的位置
    private boolean shape;
    public QuanzQzBimp quanzQzBimp;
    private Context context;

    public boolean isShape() {
        return shape;
    }

    public void setShape(boolean shape) {
        this.shape = shape;
    }

    public QuanzQzPublishGridAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void update() {
       // loading();
    }

    public int getCount() {
        return (quanzQzBimp.bmp.size() + 1);
    }

    public Object getItem(int arg0) {

        return null;
    }

    public long getItemId(int arg0) {

        return 0;
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public void setItem(QuanzQzBimp bimp){
        this.quanzQzBimp = bimp;
        Log.i("sxun", "gridview:"+quanzQzBimp.drr.size());
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    /**
     * ListView Item设置
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        final int coord = position;
        ViewHolder holder = null;
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.quanz_qz_bimp,
                    parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView
                    .findViewById(R.id.publish_grida_image);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

//        if (position == quanzQzBimp.bmp.size()) {
//            holder.image.setImageBitmap(BitmapFactory.decodeResource(
//                    context.getResources(), R.drawable.icon_addpic_unfocused));
//            if (position == 9) {
//                holder.image.setVisibility(View.GONE);
//            }
//        } else {
//            holder.image.setImageBitmap(QuanzQzBimp.bmp.get(position));
//        }
        if(position == quanzQzBimp.bmp.size()){
                holder.image.setVisibility(View.GONE);
            return convertView;
        }
        if (quanzQzBimp.bmp.size() > 0){
            holder.image.setImageBitmap(quanzQzBimp.bmp.get(position));
            holder.image.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    public class ViewHolder {
        public ImageView image;
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                   // adapter.notifyDataSetChanged();
                    break;
            }
            super.handleMessage(msg);
        }
    };

//    public void loading() {
//        new Thread(new Runnable() {
//            public void run() {
//                while (true) {
//                    if (quanzQzBimp.max == quanzQzBimp.drr.size()) {
//                        Message message = new Message();
//                        message.what = 1;
//                        handler.sendMessage(message);
//                        break;
//                    } else {
//                        try {
//                            String path = quanzQzBimp.drr.get(quanzQzBimp.max);
//                            System.out.println(path);
//                            Bitmap bm = quanzQzBimp.revitionImageSize(path);
//                            quanzQzBimp.bmp.add(bm);
//                            String newStr = path.substring(
//                                    path.lastIndexOf("/") + 1,
//                                    path.lastIndexOf("."));
//                            FileUtils.saveBitmap(bm, "" + newStr);
//                            Bimp.max += 1;
//                            Message message = new Message();
//                            message.what = 1;
//                            handler.sendMessage(message);
//                        } catch (IOException e) {
//
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//        }).start();
//    }

}
